/*
 * @Author: 徐建辰
 * @Date: 2021-11-23 14:29:55
 * @LastEditTime: 2021-11-23 15:55:44
 * @LastEditors: Please set LastEditors
 * @Description: 生产阶段配置
 */
const webpack = require('webpack')
const {merge} = require('webpack-merge')
const CssMinimizerPlugin = require('mini-css-extract-plugin')
const CompressionPlugin = require('compression-webpack-plugin')
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
const env = require('../env/prod.env')
const baseWebpackConfig = require('./webpack.base')

module.exports = merge(baseWebpackConfig, {
  mode: 'production',
  plugins:[
    new BundleAnalyzerPlugin(),
    new CompressionPlugin(),
    new CssMinimizerPlugin(),
    new webpack.DefinePlugin({
      "process.env": env,
    }),
  ],
  module: {
    rules: [
      // 配置html-loader：将HTML导出为字符串，处理HTML中引入的静态资源
      {
        test: /\.html$/i,
        loader: "html-loader",
        // options: {
        //   esModule: false, //在开发环境中启用false
        // },
      },
    ]
  },
  optimization: {
    // 拆分chunk
    splitChunks: {
      // 把动态和非动态模块同时进行优化打包，所有模块都扔到vendors.bundle.js里面
      chunks: 'all',
      name: 'vendor',
      // 将chunks按照cacheGroups中给定的条件分组输出
      cacheGroups: {
        'echarts.vendor': {
          name: 'echarts.vendor',
          priority: 40,
          test: /[\\/]node_modules[\\/](echarts|zrender)[\\/]/,
          // 优先级，默认组的优先级为负，自定义组的默认值为0
          chunks: 'all',
        },
        lodash: {
          name: 'lodash',
          chunks: 'async',
          test: /[\\/]node_modules[\\/]lodash[\\/]/,
          priority: 40,
        },
        'async-common': {
          // 把动态模块打包进vender，非动态模块保持原样（不优化）
          chunks: 'async',
          minChunks: 2,
          name: 'async-commons',
          priority: 30,
        },
        commons: {
          name: 'commons',
          chunks: "all",
          minChunks: 2,
          priority: 20,
        },
      }
    }
  }
})
